<?php


namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\User;
use app\admin\model\UserLoginLog;
use app\admin\model\Sign;
use app\admin\model\CashOut;
use app\admin\model\AdLog;
use app\admin\model\Statistics;
use app\admin\model\TaskStatistics;
use app\admin\model\UrgeStats;
use app\admin\model\Duration;
use app\admin\model\UserKeepDay;
use app\admin\model\DurationRecord;

use think\Db;


class Crontab extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    // 每日00:05执行
    public function initDay() {
        $this->statistics();
        $this->Usertask();
        $this->nodeStats();
        $this->gameTime();
    }
    /*
     * 每日基本数据统计
     *
     * */
    public function statistics()
    {

        $appList = Db::name('app')->where('status', 0)->column('app_id');
        $list = [];
        //dump($this->sum(11));die;
        foreach ($appList as $val) {
            //$this->insertStatistics($val);
            $list[] = $this->sum($val);
        }
       Statistics::insertAll($list);

    }
  /*  public function insertStatistics($app_id) {
        $time = 1615132800;
        for ($i = 0; $i < 120; $i++) {
            $task['app_id'] = $app_id;
            $task['date'] = date('Y-m-d', $time+$i*24*60*60);
            $task['task_1'] = mt_rand(1,10000);
            $task['task_2'] = mt_rand(1,10000);
            $task['task_3'] = mt_rand(1,10000);
            $task['task_4'] = mt_rand(1,10000);
            $task['task_5'] = mt_rand(1,10000);
            $task['task_6'] = mt_rand(1,10000);
            TaskStatistics::insert($task);
            /*$statistics['app_id'] = $app_id;
            $statistics['date'] = date('Y-m-d', $time+$i*24*60*60);
            $statistics['new'] = mt_rand(1,10000);
            $statistics['login'] = mt_rand(1,10000);
            $statistics['sdk'] = mt_rand(1,10000);
            $statistics['sign'] = mt_rand(1,10000);
            $statistics['double_sign'] = mt_rand(1,10000);
            $statistics['apply'] =  mt_rand(1,10000);
            $statistics['adopt'] = mt_rand(1,10000);
            $statistics['click_coin'] = mt_rand(1,10000);
            $statistics['click_dollar'] = mt_rand(1,10000);
            $statistics['click_cashout'] = mt_rand(1,10000);
            $statistics['advert'] = mt_rand(1,10000);
            $statistics['advert_fill'] = lcg_value(0,1)*100;
            $statistics['advert1'] = mt_rand(1,10000);
            $statistics['advert1_fill'] = mt_rand(1,10000);
            $statistics['advert2'] = lcg_value(0,1);
            $statistics['advert2_fill'] = lcg_value(0,1)*100;
            $statistics['advert3'] = mt_rand(1,10000);
            $statistics['advert3_fill'] = lcg_value(0,1)*100;
            $statistics['advert4'] = mt_rand(1,10000);
            $statistics['advert4_fill'] = lcg_value(0,1)*100;
            Statistics::insert($statistics);
        }

    }
    public function insert($app_id){
        Db::startTrans();
        $time = time() - 30*60*60;
        $date = date( 'Y-m-d H:i:s', $time);
        $uid =  Db::name('user')->insertGetId(['app_id' => $app_id, 'createtime' => $time]);
        UserLoginLog::insert(['app_id' => $app_id, 'user_id' => $uid, 'addtime' => $date, 'type' => 2]);
        UserLoginLog::insert(['app_id' => $app_id, 'user_id' => $uid, 'addtime' => $date, 'type' => 1]);
        Sign::insert(['app_id' => $app_id, 'user_id' => $uid, 'sign_day' => $date, 'type' => 1]);
        Sign::insert(['app_id' => $app_id, 'user_id' => $uid, 'sign_day' => $date, 'type' => 2]);
        CashOut::insert(['app_id' => $app_id, 'user_id' => $uid, 'addtime' => $date, 'ordersn' => create_order_code(), 'price' => '0.04', 'type' => 2, 'status' => 0]);
        CashOut::insert(['app_id' => $app_id, 'user_id' => $uid, 'addtime' => $date, 'ordersn' => create_order_code(), 'price' => '0.04', 'updatetime'=> $date, 'type' => 2, 'status' => 1]);
        CashOut::insert(['app_id' => $app_id, 'user_id' => $uid, 'addtime' => $date, 'ordersn' => create_order_code(), 'price' => '100000', 'type' => 1, 'status' => 0]);
        CashOut::insert(['app_id' => $app_id, 'user_id' => $uid, 'addtime' => $date, 'ordersn' => create_order_code(), 'price' => '200000', 'updatetime'=> $date, 'type' => 1, 'status' => 1]);
        CashOut::insert(['app_id' => $app_id, 'user_id' => $uid, 'addtime' => $date, 'ordersn' => create_order_code(), 'price' => '100', 'type' => 0, 'status' => 0]);
        CashOut::insert(['app_id' => $app_id, 'user_id' => $uid, 'addtime' => $date, 'ordersn' => create_order_code(), 'price' => '200', 'updatetime'=> $date, 'type' => 0, 'status' => 1]);
        AdLog::insert(['app_id' => $app_id, 'user_id' => $uid, 'addtime' => $date, 'come' => 1, 'adtype' => 0, 'status' => 1, 'play' => 1]);
        AdLog::insert(['app_id' => $app_id, 'user_id' => $uid, 'addtime' => $date, 'come' => 1, 'adtype' => 0, 'status' => 0, 'play' => 0]);
        AdLog::insert(['app_id' => $app_id, 'user_id' => $uid, 'addtime' => $date, 'come' => 1, 'adtype' => 0, 'status' => 1, 'play' => 0]);
        AdLog::insert(['app_id' => $app_id, 'user_id' => $uid, 'addtime' => $date, 'come' => 0, 'adtype' => 1, 'status' => 1, 'play' => 1]);
        AdLog::insert(['app_id' => $app_id, 'user_id' => $uid, 'addtime' => $date, 'come' => 0, 'adtype' => 1, 'status' => 0, 'play' => 0]);
        AdLog::insert(['app_id' => $app_id, 'user_id' => $uid, 'addtime' => $date, 'come' => 0, 'adtype' => 1, 'status' => 1, 'play' => 0]);
        AdLog::insert(['app_id' => $app_id, 'user_id' => $uid, 'addtime' => $date, 'come' => 0, 'adtype' => 2, 'status' => 1, 'play' => 1]);
        AdLog::insert(['app_id' => $app_id, 'user_id' => $uid, 'addtime' => $date, 'come' => 0, 'adtype' => 2, 'status' => 0, 'play' => 0]);
        AdLog::insert(['app_id' => $app_id, 'user_id' => $uid, 'addtime' => $date, 'come' => 0, 'adtype' => 2, 'status' => 1, 'play' => 0]);
        AdLog::insert(['app_id' => $app_id, 'user_id' => $uid, 'addtime' => $date, 'come' => 0, 'adtype' => 3, 'status' => 1, 'play' => 1]);
        AdLog::insert(['app_id' => $app_id, 'user_id' => $uid, 'addtime' => $date, 'come' => 0, 'adtype' => 3, 'status' => 0, 'play' => 0]);
        AdLog::insert(['app_id' => $app_id, 'user_id' => $uid, 'addtime' => $date, 'come' => 0, 'adtype' => 3, 'status' => 1, 'play' => 0]);
        Db::commit();
    }*/

    protected function sum($app_id) {
        $startTime = strtotime('yesterday');
        $endTime = $startTime + 24*60*60;
        $range = $startTime . ',' . $endTime;
        $where['app_id'] = $app_id;
        $where = array_filter($where);

        $statistics = [];
        $statistics['app_id'] = empty($app_id) ? 0 : $app_id;
        //日期
        $statistics['date'] = date('Y-m-d',$startTime);
        //每日新增用户数量
        $statistics['new'] = User::where($where)->where('createtime','between', $range)->count();
        //每日登录人数数量
        $statistics['login'] = UserLoginLog::where($where)
            ->whereTime('addtime', 'yesterday')
            ->group('user_id')
            ->count();
        //进入sdk的数量
        $statistics['sdk'] = UserLoginLog::where($where)
            ->whereTime('addtime', 'yesterday')
            ->where('type', '>',1)
            ->group('user_id')
            ->count();
        //签到人数
        $statistics['sign'] = Sign::where($where)
            ->whereTime('sign_day', 'yesterday')
            ->count();
        //双倍签到人数
        $statistics['double_sign'] = Sign::where($where)
            ->whereTime('sign_day', 'yesterday')
            ->where('type', 1)
            ->count();
        //paypal验证申请人数
        $statistics['apply'] = CashOut::where($where)
            ->whereTime('addtime', 'yesterday')
            ->where('type', 2)
            ->count();
        //验证申请通过人数
        $statistics['adopt'] = CashOut::where($where)
            ->whereTime('updatetime', 'yesterday')
            ->where('type', 2)
            ->where('status',1)
            ->count();
        //金币提现点击次数
        $statistics['click_coin'] = CashOut::where($where)
            ->whereTime('addtime', 'yesterday')
            ->where('type', 1)
            ->count();
        //美元提现点击次数
        $statistics['click_dollar'] = CashOut::where($where)
            ->whereTime('addtime', 'yesterday')
            ->where('type', 0)
            ->count();
        //提现申请次数
        $statistics['click_cashout'] = CashOut::where($where)
            ->whereTime('addtime', 'yesterday')
            ->where('type', '<>',2)
            ->count();
        //广告播放次数
        $statistics['advert'] = AdLog::where($where)
            ->whereTime('addtime', 'yesterday')
            ->where('play', 1)
            ->count();
        //总广告填充率
        $total = AdLog::where($where)->whereTime('addtime', 'yesterday')->count();
        $advert_satus = AdLog::where($where)->whereTime('addtime', 'yesterday')->where('status', 1)->count();
        if ($advert_satus) {
            $statistics['advert_fill'] = number_format($advert_satus/$total,4)*100;
        } else{
            $statistics['advert_fill'] = null;
        }

        //1激励广告播放数量
        $statistics['advert1'] =  AdLog::where($where)
            ->whereTime('addtime', 'yesterday')
            ->where('play', 1)
            ->where('adtype', 0)
            ->count();
        //1激励广告填充率
        $total = AdLog::where($where)->whereTime('addtime', 'yesterday')->where('adtype', 0)->count();
        $advert1_satus= AdLog::where($where)->whereTime('addtime', 'yesterday')->where('adtype', 0)->where('status', 1)->count();
        if ($advert1_satus) {
            $statistics['advert1_fill'] = number_format($advert1_satus/$total,4)*100;
        } else{
            $statistics['advert1_fill'] = null;
        }
        //2插屏广告播放数量
        $statistics['advert2'] = AdLog::where($where)
            ->whereTime('addtime', 'yesterday')
            ->where('play', 1)
            ->where('adtype', 1)
            ->count();
        //2插屏广告播填充率
        $total = AdLog::where($where)->whereTime('addtime', 'yesterday')->where('adtype', 1)->count();
        $advert2_satus = AdLog::where($where)->whereTime('addtime', 'yesterday')->where('adtype', 1)->where('status', 1)->count();
        if ($advert2_satus) {
            $statistics['advert2_fill'] = number_format($advert2_satus/$total,4)*100;
        } else{
            $statistics['advert2_fill'] = null;
        }
        //3banner广告播放数
        $statistics['advert3'] = AdLog::where($where)
            ->whereTime('addtime', 'yesterday')
            ->where('play', 1)
            ->where('adtype', 2)
            ->count();
        //3banner广告填充率
        $total = AdLog::where($where)->whereTime('addtime', 'yesterday')->where('adtype', 2)->count();
        $advert3_satus = AdLog::where($where)->whereTime('addtime', 'yesterday')->where('adtype', 2)->where('status', 1)->count();
        if ($advert3_satus) {
            $statistics['advert3_fill'] = number_format($advert3_satus/$total,4)*100;
        } else{
            $statistics['advert3_fill'] = null;
        }
        //4积分墙广告填充
        $statistics['advert4'] = AdLog::where($where)
            ->whereTime('addtime', 'yesterday')
            ->where('play', 1)
            ->where('adtype', 3)
            ->count();
        //4积分墙广告填充率
        $total = AdLog::where($where)->whereTime('addtime', 'yesterday')->where('adtype', 3)->count();
        $advert4_satus = AdLog::where($where)->whereTime('addtime', 'yesterday')->where('adtype', 3)->where('status', 1)->count();
        if ($advert4_satus) {
            $statistics['advert4_fill'] = number_format($advert4_satus/$total,4)*100;
        } else{
            $statistics['advert4_fill'] = null;
        }
        return $statistics;
    }
    /*
     * 每日用户任务统计
     * */
    public function  Usertask()
    {
        $appList = Db::name('app')->where('status', 0)->column('app_id');
        $taskList = Db::name('task')->order('sku')->column('sku');
        $date = date('Y-m-d',strtotime('yesterday'));
        foreach ($appList as $val) {
            $list = ['app_id' => $val, 'date' => $date];
            $i = 1;
            foreach ($taskList as $item) {
                $key = 'task_'.$i;
                $list[$key] = DB::name('finish_task')->where('app_id', $val)->whereTime('addtime', 'yesterday')->where('task_sku', $item)->count();
                $i++;
            }
            TaskStatistics::insert($list);
        }
    }

    /*
     * 每日模块游戏广告点统计
     * */
    public function nodeStats()
    {
        $appList = Db::name('app')->where('status', 0)->column('app_id');
        $gameList = Db::name('game')->where('status', 1)->column('id');
        $data = [];
        foreach ($appList as $v) {
            //sdk总激励广告次数
            $tol = AdLog::where('app_id', $v)
                ->where('node', '<>', 1)
                ->where('status',  1)
                ->where('adtype', 0)
                ->whereTime('addtime', 'yesterday')
                ->count();
            foreach ($gameList as $key => $val) {

                $nodeList = Db::name('node')
                    ->where('game_id', $val)
                    ->where('node_id', '<>', 1)
                    ->where('status', 1)
                    ->column('node_id');
                //sdk模块游戏次数
                $count = AdLog::where('app_id', $v)
                    ->whereIn('node', $nodeList)
                    ->where('adtype', 0)
                    ->where('status',  1)
                    ->whereTime('addtime', 'yesterday')
                    ->count();

                $data[$key]['date'] = date('Y-m-d', strtotime('yesterday'));
                $data[$key]['app_id'] = $v;
                $data[$key]['game_id'] = $val;
                $data[$key]['count'] = $count;
                if ($tol == 0 || $count == 0) {
                    $data[$key]['ratio'] = 0;
                } else {
                    $data[$key]['ratio'] = ($count/$tol)*100;
                }
                if ($count == 0) {
                    $data[$key]['details'] = null;
                } else {
                    $temp = [];
                    foreach ($nodeList as $item) {
                         $nodeCount = AdLog::where('app_id', $v)
                             ->whereIn('node', $item)
                             ->where('status',  1)
                             ->whereTime('addtime', 'yesterday')
                             ->where('adtype', 0)
                             ->count();
                         if ($nodeCount) {
                             $temp[$item] = $nodeCount;
                         }

                    }
                    $data[$key]['details']  = json_encode($temp);
                }

            }
            //dump($data);
            UrgeStats::insertAll($data);
        }

    }


    /*
     *
     * 每日人均玩法时长统计
     * */
    public function gameTime()
    {
        $appList = Db::name('app')->where('status', 0)->column('app_id');
        $gameList = Db::name('game')->where('id', '<>', 6)->where('status', 1)->column('id');
        $data = [];
        foreach ($appList as $item) {
            $tol = Db::name('duration_record')
                ->where('app_id', $item)
                ->where('game_id', 7)
                ->where('duration', '>', 0)
                ->whereTime('addtime', 'yesterday')
                ->count('id');
            $time = Db::name('duration_record')
                ->where('app_id', $item)
                ->where('game_id', 7)
                ->where('duration', '>', 0)
                ->whereTime('addtime', 'yesterday')
                ->sum('duration');
            foreach ($gameList as $k => $v) {
                $data[$k]['date'] = date('Y-m-d', strtotime('yesterday'));
                $data[$k]['app_id'] = $item;
                $data[$k]['game_id'] = $v;
                $gameTol = Db::name('duration_record')
                    ->where('app_id', $item)
                    ->where('game_id', $v)
                    ->where('duration', '>', 0)
                    ->whereTime('addtime', 'yesterday')
                    ->count('id');
                $gameTime = Db::name('duration_record')
                    ->where('app_id', $item)
                    ->where('game_id', $v)
                    ->where('duration', '>', 0)
                    ->whereTime('addtime', 'yesterday')
                    ->sum('duration');
               // echo $gameTime;echo $gameTol;
                if ($gameTol == 0) {
                    $data[$k]['avg'] = null;
                } else {
                    $data[$k]['avg'] = ($gameTime/$gameTol)/60;
                }
                if ($tol == 0) {
                    $data[$k]['ratio'] = null;
                } else {
                    $data[$k]['ratio'] = (($gameTime/$gameTol)/($time/$tol))*100;
                }

            }
            //dump($data);
            Duration::insertAll($data);
            //dump($res);die;
        }
    }
}
?>